//
//  BwhcViewController.m
//  YiqiMobile
//
//  Created by henry on 13-4-6.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "BoweiListViewController.h"
#import "Config.h"
#import "Utility.h"
#import "QuartzCore/QuartzCore.h"
#import "BoweiListViewCell.h"
#import "DefineAction.h"
#import "JSON.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
#import "BowenPreviewViewController.h"

@interface BoweiListViewController ()

@end

@implementation BoweiListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.navigationItem.titleView=[Utility getTitleView:@"圈子博文"];
    self.arrayList=[NSMutableArray array];
    
    /*self.bwhcTopMenu=[[BwhcTopMenu alloc] initWithFrame:CGRectMake(0, 0, 320, 38)];
    self.bwhcTopMenu.delegate=self;
    NSMutableArray *param=[NSMutableArray array];
    [param addObject:@{@"name":@"全部",@"id":@"1"}];
    [param addObject:@{@"name":@"技术",@"id":@"2"}];
    [param addObject:@{@"name":@"小资",@"id":@"3"}];
    [param addObject:@{@"name":@"咖啡",@"id":@"4"}];
    [param addObject:@{@"name":@"娱乐",@"id":@"5"}];
     [param addObject:@{@"name":@"社会",@"id":@"5"}];
     [param addObject:@{@"name":@"八卦",@"id":@"5"}];
    [param addObject:@{@"name":@"更多",@"id":@"6"}];
    [self.bwhcTopMenu showData:param andPadding:15];
    [self.view addSubview:self.bwhcTopMenu];*/
    
	// Do any additional setup after loading the view.
    
    float height=367;
    if (iPhone5) {
        height+=88;
    }
    CGRect tableViewFrame=CGRectMake(0,0,UISCREEN_WIDTH, height+49);
    self.mytableView=[[UITableView alloc] initWithFrame:tableViewFrame];
    self.mytableView.tag=HOMEPAGETABLEVIEWTAG;
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    
}
-(void) loadDataFromServer:(NSString *)quanziId andPage:(int)page{
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],QUANZI_ALLBOWEN];
    NSString *userId=[[NSUserDefaults standardUserDefaults] objectForKey:USERID];
    NSMutableDictionary *param=[NSMutableDictionary dictionaryWithDictionary:@{@"type":[NSString stringWithFormat:@"%d",page],@"id":quanziId,@"u.u0":userId}];
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
    [[MBProgressHUD showHUDAddedTo:self.view animated:YES] setLabelText:LOADING_TITLE_DEFAULT];
    [self.request startLoadData];
}


- (void)requestFinished:(id)context{
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result  objectForKey:@"code"] integerValue]==0) {
            if ([[result  objectForKey:@"method"] isEqualToString:QUANZI_ALLBOWEN]) {
                [self.arrayList removeAllObjects];
                [self.arrayList addObjectsFromArray:[result objectForKey:@"list"]];
                [self.mytableView reloadData];
            }
            
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:NO];
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}

//表视图委托
#pragma mark -
#pragma mark table view data source methods

//返回某个表视图有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayList count];
}

//设置行高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}

//表视图显示表视图项时调用：第一次显示（根据视图大小显示多少个视图项就调用多少次）以及拖动时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"boweiListViewCell";
    
    BoweiListViewCell *cell =(BoweiListViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[BoweiListViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: CellIdentifier];
    }
    NSDictionary *dict=[self.arrayList objectAtIndex:indexPath.row];
    cell.titleLabel.text=[dict objectForKey:@"str2"];
    cell.tongjiLabel.text=[NSString stringWithFormat:@"阅读：%@  评论：%@  收藏  %@",[dict objectForKey:@"count3"],[dict objectForKey:@"count5"],[dict objectForKey:@"count6"]];
    
    cell.zuozheLabel.text=[NSString stringWithFormat:@"发布:%@",[Utility datefromSeconds:[dict objectForKey:@"dat4"] andDateFormat:@"yyyy-MM-dd HH:mm"]];
   /* for (UIView *view in cell.biaoqianView.subviews) {
        [view removeFromSuperview];
    }
    for (int i=0;i<((indexPath.row%3)+1);i++) {
        NSString *tagname=@"小资";
        CGSize size=[Utility getStringSize:tagname font:[UIFont fontWithName:FONT_FAMILY size:9.0] withMaxSize:CGSizeMake(200, 5)];
        UILabel *label=[Utility detailTextLabel:CGRectMake(i*(size.width+28), 1, size.width+22, 13) andFontSize:9.0];
        label.backgroundColor=[UIColor colorWithRed:73.0/255 green:133.0/255 blue:248.0/255 alpha:1.0];
        label.textColor=[UIColor whiteColor];
        label.layer.cornerRadius=2;
        label.textAlignment=UITextAlignmentCenter;
        label.text=tagname;
        [cell.biaoqianView addSubview:label];
    }*/
    return cell;
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        BowenPreviewViewController *bowen=[[BowenPreviewViewController alloc] initWithNibName:nil bundle:nil];
        NSDictionary *dcit=[self.arrayList objectAtIndex:indexPath.row];
        [bowen loadDataFromServer:[dcit objectForKey:@"str1"]];
        [self.navigationController pushViewController:bowen animated:YES];
        
}



-(void) onclickScrollButton:(NSUInteger)category{
    NSLog(@"buttontag=%d",category);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
